package com.Telien.app.ui.main;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import com.Telien.app.request.object.DriverRequest;
import com.Telien.app.ui.user.UserLogInDialog;
import com.Telien.app.user.object.User;
import com.Telien.app.user.service.UserManagementService;


public class MainApp implements ActionListener{

	private JFrame frame = new JFrame();
	private UserLogInDialog uld;
	private UserManagementService ums;
	private JMenuItem createRideMenuItem;
	private JMenuItem exitMenuItem;
	private User user;
	private JTabbedPane tabbedPane; 
	private MyRequestsPanel mrp;
	private RequestTablePanel rtp;
	private DriverRideRequestPanel drrp;
	/**
	 * Launch the application.
	 *
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainApp window = new MainApp();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public MainApp(User usr) {
		user = usr;
		initialize();
		uld = new UserLogInDialog();
		frame.setVisible(false);
	}
	
	public MainApp() {
		initialize();
		uld = new UserLogInDialog();
		
		frame.setVisible(true);
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 735, 581);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JMenuBar menuBar = new JMenuBar();
		frame.setJMenuBar(menuBar);
		
		JMenu mnFile = new JMenu("File");
		menuBar.add(mnFile);
		
		JMenu mnHelp = new JMenu("Help");
		menuBar.add(mnHelp);
		
		JMenuItem mntmNewMenuItem = new JMenuItem("About");
		mnHelp.add(mntmNewMenuItem);
		GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGap(0, 717, Short.MAX_VALUE)
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGap(0, 510, Short.MAX_VALUE)
		);
		frame.getContentPane().setLayout(groupLayout);
		tabbedPane = new JTabbedPane();
		JPanel createDriverPanel = new CreateRide(user);
		tabbedPane.addTab("Create Ride Session", createDriverPanel);
		//JPanel jplInnerPanel2 = createInnerPanel("Will contain Requests UI");
		rtp = new RequestTablePanel(user);
		tabbedPane.addTab("View Driver Requests", rtp);
		//JPanel jplInnerPanel3 = createInnerPanel("Will contain Search UI");
		mrp = new MyRequestsPanel(user);
		tabbedPane.addTab("View My Ride Session", mrp);
		drrp = new DriverRideRequestPanel(user);
		tabbedPane.addTab("View My Driver Session", drrp);
		//JPanel jplInnerPanel4 = createInnerPanel("Will contain User Details UI");
		
		JPanel jplInnerPanel5 = new LocationPanel();
		tabbedPane.addTab("Get Location", jplInnerPanel5);
		
		JPanel jplInnerPanel4 = new UserDetailsPanel(user);
		tabbedPane.addTab("User Details", jplInnerPanel4);
		
		tabbedPane.setVisible(true);
		JPanel contentPane = new JPanel(new BorderLayout());
		contentPane.add(tabbedPane);
		frame.setContentPane(contentPane);
		
		
		/*
		createRideMenuItem = new JMenuItem("Create Ride");
		createRideMenuItem.addActionListener(this);
		mnFile.add(createRideMenuItem);
		*/
		exitMenuItem = new JMenuItem("Exit");
		exitMenuItem.addActionListener(this);
		mnFile.add(exitMenuItem);
	}
	
	public JFrame getFrame(){
		return frame;
	}
	
	protected JPanel createInnerPanel(String text) {
		JPanel jplPanel = new JPanel();
		JLabel jlbDisplay = new JLabel(text);
		jlbDisplay.setHorizontalAlignment(JLabel.CENTER);
		jplPanel.setLayout(new GridLayout(1, 1));
		jplPanel.add(jlbDisplay);
		return jplPanel;
	}
	
	public UserLogInDialog getUserLogInDialog(){
		return uld;
	}
	
	public void actionPerformed(ActionEvent e){
		if (e.getSource() == createRideMenuItem){
			CreateRide createRide = new CreateRide(user);
		}
		else if (e.getSource() == exitMenuItem){
			System.exit(0);
		}
	}
	
	public void setUser(User usr){
		user = usr;
	}
		
	// Add request from RequestTablePanel to MyRequestPanel
	public void addToMyRequests(DriverRequest request, int rideSessionId) {
		mrp.addRequest(request, rideSessionId);
		refreshDriverRideRequestPanel();
		reloadMyRequests();
	}
	
	public void reloadMyRequests() {
		mrp = new MyRequestsPanel(user, mrp.getRequests(), mrp.getMatchingSessionIds());
		tabbedPane.removeTabAt(2);
		tabbedPane.insertTab("My Ride Requests", null, mrp, null, 2);
		tabbedPane.revalidate();
		tabbedPane.repaint();
		tabbedPane.setSelectedIndex(2);
	}
	
	public void refreshDriverRideRequestPanel() {
		drrp = new DriverRideRequestPanel(user);
		tabbedPane.removeTabAt(3);
		tabbedPane.insertTab("Driver Ride Requests", null, drrp, null, 3);
		tabbedPane.revalidate();
		tabbedPane.repaint();
		tabbedPane.setSelectedIndex(3);
	}
	
	public void refreshRequestPanel() {
		rtp = new RequestTablePanel(user);
		tabbedPane.removeTabAt(1);
		tabbedPane.insertTab("Search For Ride", null, rtp, null, 1);
		tabbedPane.revalidate();
		tabbedPane.repaint();
		tabbedPane.setSelectedIndex(1);		
	}
}
